﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MovieRental.DataInterfaces;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using System.Configuration;

namespace MovieRental.BuisnessComponents
{
    public class ComponentFactory
    {
        private static IRepositoryFactory _repositoryFactory;
        private IUnityContainer _container;

        private static ComponentFactory _instance;

        private ComponentFactory()
        {
            _container = new UnityContainer().LoadConfiguration();
            _repositoryFactory = _container.Resolve<IRepositoryFactory>();
            SeedDefaultMediaTypes();
        }

        public void SeedDefaultMediaTypes()
        {
            var mediaTypeComponent = GetMediaTypeComponent();
            if (!mediaTypeComponent.GetAll().Any())
            {
                mediaTypeComponent.Add("Blue-Ray", 5);
                mediaTypeComponent.Add("DVD", 3);
                mediaTypeComponent.Add("VHS", 1.5);
                mediaTypeComponent.Add("Video CD", 2);
                mediaTypeComponent.Add("HD-DVD", 6);
            }

        }

        public void ReconnectDataSource()
        {
            _repositoryFactory.Reconnect();
        }

        public static ComponentFactory Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new ComponentFactory();
                return _instance;
            }
        }

        public CustomerComponent GetCustomerComponent()
        {
            return new CustomerComponent(_repositoryFactory.GetCustomerRepository());
        }

        public LoanCartComponent GetLoanCartComponent()
        {
            return new LoanCartComponent(_repositoryFactory.GetLoanCartRepository());
        }

        public CartDiscountComponent GetCartDiscountComponent()
        {
            return new CartDiscountComponent(_repositoryFactory.GetCartDiscountRepository());
        }

        public PersonalDiscountComponent GetPersonalDiscountComponent()
        {
            return new PersonalDiscountComponent(_repositoryFactory.GetPersonalDiscountRepository());
        }

        public TemporaryDiscountComponent GetTemporaryDiscountComponent()
        {
            return new TemporaryDiscountComponent(_repositoryFactory.GetTemporaryDiscountRepository());
        }

        public EditionComponent GetEditionComponent()
        {
            return new EditionComponent(_repositoryFactory.GetEditionRepository());
        }

        public MovieComponent GetMovieComponent()
        {
            return new MovieComponent(_repositoryFactory.GetMovieRepository());
        }

        public MediaTypeComponent GetMediaTypeComponent()
        {
            return new MediaTypeComponent(_repositoryFactory.GetMediaTypeRepository());
        }
    }
}
